<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>public/skin/back-end/css/tablemain.css"/>
<?php $title_show = empty ($datacontent['id'])?"Add":"Edit"; ?>
<h1>Menu Item <span class="greysubheading"><?php echo $title_show; ?></span></h1>
<form id="menu-item-form" action="" method="POST">
    <input type="hidden" name="menu_id" value="<?php echo $menu_id ?>">
<table width="100%" cellspacing="1">
<thead>
<tr>
<th scope="col">Fields</th>
<th scope="col">Data's</th>
</tr>
</thead>
<tbody>
<?php echo textfield('title','Title',((array_key_exists('title', $datacontent))?$datacontent['title']:null))?>
<?php echo textfield('link','Link',((array_key_exists('link', $datacontent))?$datacontent['link']:null))?>
<tr>
    <th scope="row">On Menu</th>
    <td align="left"><?php echo $menu_id ?></td>
</tr>
<tr>
    <th scope="row">Parent Node:</th>
    <td align="left">
        <select name="parent_id">
            <?php foreach ($alltree as $item) {
                if (in_array($item['id'], $allchildarr )) continue;
                if ($item['id']==$parentNode['id']){
                    echo "<option selected='selected' value='{$item['id']}'>{$item['title']}</option>";
                }else{
                    echo "<option value='{$item['id']}'>{$item['title']}</option>";
                }                
            }?>
        </select>
    </td>
</tr>
<tr id="position" style="display: none">
  
</tr>
</tbody>
<tfoot>
<tr>
<th scope="row"></th>
<td><!--pagination -->
<div id="central" align="center">
<table>
<tr>
<td><input type="submit" id="button" value="Edit" /></td>
<td><input type="reset" id="reset" value="Reset" onClick="location.reload(true)"/></td>
</tr>
</table>
</div></td>
</tr>
</tfoot>
</table>
<p>&nbsp;</p>
</form>
<script>
$(document).ready(function(){
   $("select[name=parent_id]").change(function(){
       var item_id = $(this).val();
       $.get('/admin/menu_item/getchild',
              {id:item_id,
               menu_id:'<?php echo $menu_id ?>'               
              },
              function(data){
                  if (data !=''){
                    $("#position").html(data);
                    $("#position").css('display','');
                  }else{
                    $("#position").html("");
                    $("#position").css('display','none');
                  }
              }
       );
   });
   
   $.get('/admin/menu_item/getchild',
              {id:$("select[name=parent_id] option:selected").val(),
               menu_id:'<?php echo $menu_id ?>'
               <?php if (count($datacontent)>0) echo ",current_id:'{$datacontent['id']}'" ?>
               <?php if (count($preNode)>0) echo ",prenode_id:'{$preNode['id']}'" ?>
               <?php if (count($nextNode)>0) echo ",nextnode_id:'{$nextNode['id']}'" ?>
              },
              function(data){
                  if (data !=''){
                    $("#position").html(data);
                    $("#position").css('display','');
                  }else{
                    $("#position").html("");
                    $("#position").css('display','none');
                  }
              }
       );  
		var menuItemOptions = {
	            dataType: 'json',
	            url: '<?php echo $_SERVER['REQUEST_URI'];?>',
	            type: 'post', 
	            beforeSubmit:  function(formData, jqForm, options) {
	            	$.prompt('Please wait...',{ buttons: {}});
	        	    return true;
	            },
	            success: function (responseText, statusText) {
	                jQuery.prompt.close();
	                if(responseText.success == true){
	                	$.prompt('Update successfully.',{
	               		 submit:function(v,m){
	               			 if(v == true){
	               				 location.href = "<?php echo base_url()?>admin/menu_item/index/<?php echo $menu_id?>"
	               			 }
	               			<?php if(empty ($datacontent['id'])){?>
	               			 else{
	               				$('input[name=title]').val('');
	               				$('input[name=link]').val('');
		               		 }
	               			<?php }?>
	               			 return true;
	               		 },
	               		 buttons: { Ok:true,Cancel:false  }
	               		 });			    				
	        		}
	    			else $.prompt('Can not save!',{ buttons: { Ok: true }});
		        	return false;
	            },
	            error: function (responseText, statusText){
	                jQuery.prompt.close();
	            	$.prompt('Can not save!',{ buttons: { Ok: true }});
	            	return false;
	            }
	     };	
		$('#menu-item-form').submit(function() {
			$("#menu-item-form").ajaxSubmit(menuItemOptions); 
			  return false;
			});
});
</script>
	